home *** CD-ROM | disk | FTP | other *** search
/ Over 1,000 Windows 95 Programs / Over 1000 Windows 95 Programs (Microforum) (Disc 1).iso / 1157 / source / scmain.pas < prev    next >
Pascal/Delphi Source File  |  1996-11-07  |  10KB  |  342 lines

  1. unit Scmain;
  2. {
  3.  Copier-Utility: CompuCopier
  4.  This program demonstrate the use of the eztwain.dll (eztwain.pas).
  5.  If you have a flatbed scanner and a graphics printer, you can use
  6.  'Compu-Copier' to make copies of a sheet of paper in an easy way.
  7.  
  8.  known bugs:
  9.  - if the scanner doesn't deliver resolution information, the copy
  10.    may not have the same size as the original (at 100% user scale)
  11.  
  12.  history:
  13.  1.0a:  first release
  14.  1.0b:  Divison by zero removed, if the TWAIN-Scanner doesn't deliver
  15.         resolution information
  16.  1.0c:  Better handling of errors, if the TWAIN-Scanner doesn't deliver
  17.         resolution information
  18.  1.0d:  try to close TWAIN, if it's already open
  19.  1.0e:  DebugBox temporary added
  20.  1.0f:  Errorhandling improved (div by zero)
  21.  1.0g:  DebugBox removed
  22.  1.0h:  DebugBox added again
  23.  1.0i:  Save Setting in IniFile,
  24.         CheckBox added to switch off automatic zoom,
  25.         CheckBox added to show/hide DebugBox
  26.  
  27.  2.0:   major enhancements:
  28.         faster copy: the TWAIN dialog will stay open after a scan,
  29.                      the print is started after pressing the Scan-button in the
  30.                      TWAIN dialog
  31.         save all settings in Ini-file
  32.         two language version, more languages are possible
  33.         automatic functions can switched off:
  34.                      -'Auto scale' the automatic calculation of the size of the
  35.                        copy depending on the resolution of the scanner and the printer
  36.                      -'Fast copy' the print driver creates the multiple copies and
  37.                        not CCopier
  38.  
  39. - to compile it, install the 'DebugBox' and 'DTrans' components before
  40.  
  41. Copyright Matthias Weingart 1996, Freeware, Comments and bugreports to
  42. matthias@penthouse.boerde.de
  43.  
  44. }
  45.  
  46. interface
  47.  
  48. uses
  49.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  50.   Forms, Dialogs, StdCtrls, EzTwain, Printers, ExtCtrls, DebugBox, Spin,
  51.   Menus, IniFiles, scinfo, Buttons, Dtrans;
  52.  
  53. const
  54.   CM_PRINTBMP    = WM_USER+1211;
  55.  
  56. type
  57.   TPositions = (poTopLeft,poBottomLeft,poTopRight,poBottomRight,poCenter);
  58.   TCenterState=(tctNone, tctTopCenter, tctCenter, tctBottomCenter);
  59.  
  60.   PGlobalSettings = ^TGlobalSettings;
  61.   TGlobalSettings = record
  62.      Version: Integer;
  63.      BitmapHandle: THandle;
  64.      ZoomX, ZoomY: Single;
  65.      CenterState: TCenterState;
  66.      NoOfCopies:  Integer;
  67.      AutoScale:   Boolean;
  68.      PrinterCopies: Boolean;
  69.      PrintDevice,PrintDriver,PrintPort: array[0..255] of char;
  70.      PrintDeviceMode: THandle;
  71.   end;
  72.  
  73.   TForm1 = class(TForm)
  74.     PrinterSetupDialog1: TPrinterSetupDialog;
  75.     Dbg1: TDebugBox;
  76.     ZoomEdit: TSpinEdit;
  77.     Label1: TLabel;
  78.     MainMenu1: TMainMenu;
  79.     Datei: TMenuItem;
  80.     Druckereinstellen1: TMenuItem;
  81.     Beenden1: TMenuItem;
  82.     Label2: TLabel;
  83.     NoOfCopiesEdit: TSpinEdit;
  84.     Scannerauswahl1: TMenuItem;
  85.     CopyButton: TButton;
  86.     Label3: TLabel;
  87.     Debugfenster1: TMenuItem;
  88.     SpeedButton1: TSpeedButton;
  89.     SpeedButton2: TSpeedButton;
  90.     SpeedButton3: TSpeedButton;
  91.     SpeedButton4: TSpeedButton;
  92.     Bevel1: TBevel;
  93.     BtnPosLeftTop: TSpeedButton;
  94.     BtnPosTop: TSpeedButton;
  95.     BtnPosCenter: TSpeedButton;
  96.     BtnPosBottom: TSpeedButton;
  97.     AutoScaleCb: TCheckBox;
  98.     ber1: TMenuItem;
  99.     LanguageSwitcher1: TLanguageSwitcher;
  100.     PrinterCopiesCb: TCheckBox;
  101.     procedure FormCreate(Sender: TObject);
  102.     procedure Druckereinstellen1Click(Sender: TObject);
  103.     procedure Beenden1Click(Sender: TObject);
  104.     procedure Scannerauswahl1Click(Sender: TObject);
  105.     procedure CopyButtonClick(Sender: TObject);
  106.     procedure Debugfenster1Click(Sender: TObject);
  107.     procedure Ueber1Click(Sender: TObject);
  108.     procedure SpeedButtonClick(Sender: TObject);
  109.     procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
  110.   private
  111.     { Private-Deklarationen }
  112.     TwainIsOpen: Boolean;
  113.     procedure InitDebug;
  114.     procedure PrintFree(s: string);
  115.  
  116.     procedure UpDateInfo;
  117.     procedure UpDateButtons;
  118.  
  119.     procedure SetPosition(A: TPositions);
  120.     procedure PrintBitmap(b: HBitmap);
  121.   protected
  122.     function  GetPosBtn: TCenterState;
  123.     procedure SetPosBtn( v: TCenterState);
  124.     function  GetWndBtn: integer;
  125.     procedure SetWndBtn( v: integer);
  126.   public
  127.     { Public-Deklarationen }
  128.     Ini: TIniFile;
  129.   end;
  130.  
  131. var
  132.   Form1: TForm1;
  133.  
  134. implementation
  135.  
  136. {$R *.DFM}
  137.  
  138. procedure TForm1.FormCreate(Sender: TObject);
  139. begin
  140.     TwainIsOpen:=False;
  141.     Ini := TIniFile.Create( ChangeFileExt(Application.ExeName,'.ini' ));
  142.  
  143.     ZoomEdit.Value:=Ini.ReadInteger( 'Params', 'Zoom', 100 );
  144.     NoOfCopiesEdit.Value:=Ini.ReadInteger( 'Params', 'NoOfCopies', 1 );
  145.     AutoScaleCb.Checked:=Ini.ReadBool( 'Params', 'AutoScale', True );
  146.     PrinterCopiesCb.Checked:=Ini.ReadBool( 'Params', 'PrinterCopies', True );
  147.     SetWndBtn( Ini.ReadInteger( 'Params', 'WindowPosition', 2 ) );
  148.     SetPosBtn( TCenterState(Ini.ReadInteger( 'Params', 'CopyCenterState',0 )));
  149.  
  150.     Dbg1.Add('Start');
  151.     UpDateInfo;
  152.     UpDateButtons;
  153. end;
  154.  
  155. procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
  156. begin
  157.      Ini.WriteInteger( 'Params', 'Zoom', ZoomEdit.Value );
  158.      Ini.WriteInteger( 'Params', 'NoOfCopies', NoOfCopiesEdit.Value );
  159.      Ini.WriteBool(    'Params', 'AutoScale', AutoScaleCb.Checked );
  160.      Ini.WriteBool(    'Params', 'PrinterCopies', PrinterCopiesCb.Checked );
  161.      Ini.WriteInteger( 'Params', 'WindowPosition', GetWndBtn );
  162.      Ini.WriteInteger( 'Params', 'CopyCenterState', ord(GetPosBtn)  );
  163.  
  164.      If TwainIsOpen then TWAIN_Close;
  165.      CanClose:=True;
  166. end;
  167.  
  168. procedure TForm1.InitDebug;
  169. begin
  170.   Dbg1.Add( '----------------------' );
  171. end;
  172.  
  173. procedure TForm1.PrintFree(s: string);
  174. begin
  175.     Dbg1.Add( s );
  176. end;
  177.  
  178.  
  179. procedure TForm1.Druckereinstellen1Click(Sender: TObject);
  180. begin
  181.      PrinterSetupDialog1.Execute;
  182.      UpDateInfo;
  183. end;
  184.  
  185. procedure TForm1.Beenden1Click(Sender: TObject);
  186. begin
  187.      Close;
  188. end;
  189.  
  190. procedure TForm1.Scannerauswahl1Click(Sender: TObject);
  191. begin
  192.   TWAIN_SelectImageSource(Handle);
  193.   Dbg1.Add( 'EzTwain Version: '+ FloatToStr(round(TWAIN_EasyVersion)/100));
  194.   UpDateInfo;
  195. end;
  196.  
  197. procedure TForm1.PrintBitmap(b: HBitmap);
  198. var
  199.     i,r, max: integer;
  200.     s: string;
  201.     c: array[0..256] of char;
  202.     PSettings: PGlobalSettings;
  203.     Settings: THandle;
  204. begin
  205.   if b<>0 then begin
  206.           {save printerinformation global}
  207.           Settings := GlobalAlloc( HeapAllocFlags or GMEM_ZEROINIT, sizeof(TGlobalSettings));
  208.           if Settings<>0 then
  209.           begin
  210.            PSettings:=GlobalLock( Settings );
  211.            if PSettings<>nil then
  212.            begin
  213.              with PSettings^ do
  214.              begin
  215.               BitmapHandle:=b;
  216.               ZoomX:=ZoomEdit.Value/100;
  217.               ZoomY:=ZoomEdit.Value/100;
  218.               CenterState:=GetPosBtn;
  219.               NoOfCopies:=NoOfCopiesEdit.Value;
  220.               AutoScale:=AutoScaleCb.Checked;
  221.               PrinterCopies:=PrinterCopiesCb.Checked;
  222.               Printer.GetPrinter(PrintDevice,PrintDriver,PrintPort,PrintDeviceMode);
  223.              end;
  224.            end;
  225.            GlobalUnlock( Settings );
  226.            s:='scprn.exe'+' ';
  227.            s:=s+IntToStr( Settings )+' ';
  228.  
  229.            Dbg1.Add('Start : '+s);
  230.            r:=WinExec( StrPCopy(c, s), SW_HIDE );
  231.            Dbg1.Add('WinExec: '+IntToStr(r) );
  232.           end;
  233.   end;
  234. end;
  235.  
  236. procedure TForm1.CopyButtonClick(Sender: TObject);
  237. var
  238.     han: HBitmap;
  239. begin
  240.  TwainIsOpen:=True;
  241.  CopyButton.Enabled:=False;
  242.  InitDebug; PrintFree('Start');
  243.  Screen.Cursor := crHourGlass;
  244.  TWAIN_Open(Handle, TWAIN_ANYTYPE); PrintFree('TwainOpen');
  245.  Screen.Cursor := crDefault;
  246.  repeat
  247.                         PrintFree('BeforeTwainAquire');
  248.     han:=TWAIN_Acquire; PrintFree('TwainAquire');
  249.     Screen.Cursor := crHourGlass;
  250.     if han<>0 then
  251.     begin
  252.       PrintBitmap( han );
  253.     end;
  254.     Screen.Cursor := crDefault;
  255.     if TWAIN_LastMessage<>0 then Dbg1.Add('TwainMsg: '+IntToStr(TWAIN_LastMessage));
  256.  until (TWAIN_State<4) or (TWAIN_LastMessage=$102);
  257.  TWAIN_Close;
  258.  CopyButton.Enabled:=True;
  259. end;
  260.  
  261. procedure TForm1.UpDateInfo;
  262. begin
  263.      Label3.Caption:=Printer.Printers.Strings[Printer.PrinterIndex];
  264. end;
  265.  
  266. procedure TForm1.Debugfenster1Click(Sender: TObject);
  267. begin
  268.      Dbg1.Visible := NOT Dbg1.Visible;
  269. end;
  270.  
  271. procedure TForm1.Ueber1Click(Sender: TObject);
  272. begin
  273.      InfoBox.Show;
  274. end;
  275.  
  276.  
  277. procedure TForm1.SetPosition(A: TPositions);
  278. begin
  279.   if not (csDesigning in ComponentState) then
  280.     case A of
  281.       poTopLeft     : SetBounds(0,0,Width,Height);
  282.       poBottomLeft  : SetBounds(0,Screen.Height-Height,Width,Height);
  283.       poTopRight    : SetBounds(Screen.Width-Width,0,Width,Height);
  284.       poBottomRight :
  285.                  SetBounds(Screen.Width-Width,Screen.Height-Height,Width,Height);
  286.       poCenter:
  287.                  SetBounds((Screen.Width-Width) div 2,(Screen.Height-Height) div 2,Width,Height);
  288.     end;
  289. end;
  290.  
  291.  
  292. procedure TForm1.UpdateButtons;
  293. begin
  294.     if SpeedButton1.Down then SetPosition(poTopLeft)
  295.      else if SpeedButton2.Down then SetPosition(poTopRight)
  296.       else if SpeedButton3.Down then SetPosition(poBottomLeft)
  297.        else if SpeedButton4.Down then SetPosition(poBottomRight)
  298.         else SetPosition(poCenter);
  299. end;
  300.  
  301. procedure TForm1.SpeedButtonClick(Sender: TObject);
  302. begin
  303.      UpdateButtons;
  304. end;
  305.  
  306. function TForm1.GetWndBtn: integer;
  307. begin
  308.      Result:=0;
  309.      if SpeedButton1.Down then Result:=1;
  310.      if SpeedButton2.Down then Result:=2;
  311.      if SpeedButton3.Down then Result:=3;
  312.      if SpeedButton4.Down then Result:=4;
  313. end;
  314.  
  315. procedure TForm1.SetWndBtn( v: integer);
  316. begin
  317.      SpeedButton1.Down:= v=1;
  318.      SpeedButton2.Down:= v=2;
  319.      SpeedButton3.Down:= v=3;
  320.      SpeedButton4.Down:= v=4;
  321. end;
  322.  
  323. function TForm1.GetPosBtn: TCenterState;
  324. begin
  325.   Result:=tctNone;
  326.   if BtnPosLeftTop.Down then Result:=tctNone;
  327.   if BtnPosTop.Down then Result:=tctTopCenter;
  328.   if BtnPosCenter.Down then Result:=tctCenter;
  329.   if BtnPosBottom.Down then Result:=tctBottomCenter;
  330. end;
  331.  
  332. procedure TForm1.SetPosBtn( v: TCenterState);
  333. begin
  334.   BtnPosLeftTop.Down:= v=tctNone;
  335.   BtnPosTop.Down:= v=tctTopCenter;
  336.   BtnPosCenter.Down:= v=tctCenter;
  337.   BtnPosBottom.Down:= v=tctBottomCenter;
  338. end;
  339.  
  340.  
  341. end.
  342.